package com.umakr.demo.controller;

import com.umakr.demo.model.ChengJi;
import com.umakr.demo.model.NBTree;
import com.umakr.demo.service.NBTreeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yangliu on 2017/8/25.
 */
@Controller
@RequestMapping("/NBTreeController")
public class NBTreeController {
    @Autowired
    NBTreeService nbTreeService;

    @RequestMapping("/showTree")
    public String showTree(){
        return "StudentManage/nbtree";
    }


    @RequestMapping("/treeDate")
    @ResponseBody
    public List<NBTree> treeDate(Long tid){
        List<NBTree> list = nbTreeService.findByTid(tid);
        List<NBTree> root = new ArrayList<>();
        for(int i =0;i<list.size();i++){
            if(list.get(i).getPid() == null){
                NBTree nbTree = list.get(i);
                root.add(nbTree);
            }
        }
        System.out.println(root);
        return buildTree(root);
    }
    private List<NBTree> buildTree(List<NBTree> root) {
         for (int i=0;i<root.size();i++){
             List<NBTree> children = nbTreeService.findByPid(root.get(i).getId());
             root.get(i).setChildren(children);
         }
         return root;
    }
//   表格
    @RequestMapping("/datagridDate")
    @ResponseBody
    public List datagridDate(Long bid){
        System.out.println(bid);
        List<ChengJi> list = nbTreeService.datagridDate(bid);
        System.out.println(list);
        return list;
    }


    @RequestMapping("/updatafenshu")
    @ResponseBody
    public void updatafenshu(Long sid,String sname,String ssex,Long bid,String kname,Long fenshu,String kaoslx){
        ChengJi chengJi = new ChengJi(sid, sname, ssex, bid, kname,fenshu, kaoslx);
        Long skid = nbTreeService.showSkid(chengJi);
        System.out.println(fenshu);
        nbTreeService.updateFenshu(fenshu,skid);
        System.out.println("success");
    }


}
